const App = getApp();

import moment from '../../utils/moment'
import Dialog from '../../weapp/dialog/dialog'
import Toast from '../../weapp/toast/toast';
// components/list/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    type: Number,
    styleP: Boolean,
    strs: String,
    cusPageIndex: Number
  },

  /**
   * 组件的初始数据
   */
  data: {
    title: '我的客户',
    type: 2,    //客户类型
    timeData: {},
    remind_time: '',
    deviceList: [],    //列表
    deviceTotal: 0,    //总数
    devicePages: 0,     //总页数
    deviceIndex: 1,      //当前页 
    searchLoading: false, //加载更多
    deviceNoData: false,  //暂无更多
    searchLoadingComplete: false, //加载到底部
    see_show:0
  },
  lifetimes: {
    ready: function () {
      // this.getCustomersList()
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onChange(e) {
      this.setData({
        timeData: e.detail,
      });
      // console.log(e)
    },
    handleHomeToReport(e) {
      let { type, id, status,reportid,remindid,project_name,status_true,onpass_text } = e.currentTarget.dataset
      // if (status == 0) {
        wx.navigateTo({
          url: `../../pages/report/index?type=${type}&id=${id}&reportid=${reportid}&status=${status}&remindid=${remindid}&project_name=${project_name}&status_true=${status_true}&onpass_text=${onpass_text}`,
        })
      // }
    },
    toDetail: function (e) {
      let id = e.currentTarget.dataset.id
      let tabid = e.currentTarget.dataset.tabid
      let inspect_id = e.currentTarget.dataset.inspect_id
      wx.navigateTo({
        // url: `../../pages/detail/index?id=${id}&post_index=${this.data.cusPageIndex}&tabid=${tabid}`,
        url: `../tabkelooklist/index?inspect_id=${inspect_id}&status=list`,
      })
    },
    handleEditReoprt(e){
      let id = e.currentTarget.dataset.remindid
      wx.navigateTo({
        url: `../../pages/report/editRep?id=${id}`,
      })
    },
    filterList(arr) {
      for (let index = 0; index < arr.length; index++) {
        arr[index].pic = ''
        arr[index].create_time = arr[index].create_time.slice(5)
        arr[index].create_time = arr[index].create_time.substring(0, arr[index].create_time.length - 3);
        // this.setData({
        //   time: arr[index].rollout_expire_time * 1000,
        //   remind_time: moment(arr[index].remind_time).format('M/DD')
        // })
        switch (arr[index].level) {
          case 'A':
            arr[index].pic = '../../images/A.png'
            break;
          case 'B':
            arr[index].pic = '../../images/B.png'
            break;
          case 'C':
            arr[index].pic = '../../images/C.png'
            break;
          case 'D':
            arr[index].pic = '../../images/D.png'
            break;
          case 'T':
            arr[index].pic = '../../images/T.png'
            break;
          case 'W':
            arr[index].pic = '../../images/W.png'
            break;
          default:
            break;
        }
      }
    },
     mobileStr(v){
          return v ? String(v).substring(0, 3)+"********" : ''
        },
    /**
     * 获取首页客户列表数据
     */
    getCustomersList(keywords) {
      // console.log(this.data.cusPageIndex)
      let _this = this
      // console.log(this.type)
      // wx.showLoading({
      //   title: '加载中...',
      // })
      App._post_form('home/getHomeListData', {
        page: _this.data.deviceIndex,
        action:'inspect',
        keywords,
        type:'2'
      }, function (result) {
        console.log(result);
        // return false
        wx.hideLoading()
        _this.filterList(result.data.list)
       let Result = result.data.list
        // let Result = result.data.list.map(el=>{
        //           const mobile = _this.mobileStr(el.mobile);
        //   console.log(mobile )
        //           return {...el, mobile}
        //         })
        // 响应无数据 则提示无数据
        if (_this.data.deviceIndex == 1 && Result.length == 0) {
          _this.setData({
            deviceNoData: true,
            searchLoading: false,
            searchLoadingComplete: false
          })
        }
        if (Result.length) {
          _this.data.deviceTotal = result.data.count
          _this.data.devicePages = result.data.total_page
          let deviceListTemp = [];
          if (_this.data.deviceIndex > 1) {
            deviceListTemp = _this.data.deviceList.concat(Result);
            console.log(deviceListTemp)
            _this.setData({
              searchLoading: true, //"正在载入更多"显示
            });
          } else {
            deviceListTemp = Result;
          }
          _this.setData({
            deviceList: deviceListTemp, //获取数据数组
            deviceNoData: false //“没有数据”隐藏
          });
        }
      });
    },
  
    // 页面触底时执行
    reachBottom(keywords) {
      var _this = this;
      console.log('1123', _this.data.type)
      if (_this.data.deviceIndex < _this.data.devicePages) {
        _this.setData({
          deviceIndex: _this.data.deviceIndex + 1,   //每次触发触底事件，把deviceIndex+1
        });
          _this.getCustomersList(keywords);
      } else if (_this.data.deviceIndex == _this.data.devicePages) {
        _this.setData({
          searchLoadingComplete: true, //“已加载全部”显示
          searchLoading: false, //"正在载入更多"隐藏
        });
      }
    }
  }
})
